package com.demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo2 {
    public static void main(String[] args) {
        /*
        public static <T> void sort(List<T> list)                         排序
        public static <T> void sort(List<T> list，comparator<T> c)        根据指定的规则进行排序
        public static <T> int binarySearch (List<T> list，T key)          以二分查找法查找元素
        public static <T> void copy(List<T> dest，List<T> src)            拷贝集合中的元素
        public static <T> int fill (List<T> list,T obj)                   使用指定的元素填充集合
        public static <T> void max/min(collection<T> coll)                根据默认的自然排序获取最大/小值
        public static <T> void swap(List<?> list, int i, int j)           交换集合中指定位置的元素
         */
        //1.排序
        ArrayList<Integer> list1 = new ArrayList<>();
        Collections.addAll(list1,10,1,2,4,8,5,9,6,7,3);
        Collections.sort(list1);
        System.out.println(list1);
        System.out.println("=========");

        //2.根据指定的规则进行排序
        Collections.sort(list1, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        System.out.println(list1);
        Collections.sort(list1, (o1, o2) -> o2-o1);
        System.out.println(list1);
        System.out.println("=========");

        //3.以二分查找法查找元素
        ArrayList<Integer> list2 = new ArrayList<>();
        Collections.addAll(list2,1,2,3,4,5,6,7,8,9,10);
        System.out.println(Collections.binarySearch(list2,9));
        System.out.println(Collections.binarySearch(list2,1));
        System.out.println(Collections.binarySearch(list2,20));
        System.out.println("=========");

        //4.拷贝集合中的元素
        ArrayList<Integer> list3 = new ArrayList<>();
        ArrayList<Integer> list4 = new ArrayList<>();
        Collections.addAll(list3,1,2,3,4,5,6,7,8,9,10);
        Collections.addAll(list4,0,0,0,0,0,0,0,0,0,0,0,0);
        Collections.copy(list4,list3);
        System.out.println(list3);
        System.out.println(list4);
        System.out.println("=========");

        //5.使用指定的元素填充集合
        ArrayList<Integer> list5 = new ArrayList<>();
        Collections.addAll(list5,1,2,3,4,5,6,7,8,9,10);
        Collections.fill(list5,100);
        System.out.println(list5);
        System.out.println("=========");

        //6.根据默认的自然排序获取最大/小值
        ArrayList<Integer> list6 = new ArrayList<>();
        Collections.addAll(list6,1,2,3,4,5,6,7,8,9,10);
        System.out.println(Collections.max(list6));
        System.out.println(Collections.min(list6));
        System.out.println("=========");

        //7.交换集合中指定位置的元素
        ArrayList<Integer> list7 = new ArrayList<>();
        Collections.addAll(list7,1,2,3,4,5);
        Collections.swap(list7,0,2);
        System.out.println(list7);
    }
}
